/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test.alan.hyperblobs;

import java.util.*;
import java.util.concurrent.*;

/**
 * Provides a layer of abstraction between the updating thread and the drawing
 * thread.
 *
 * @author Michael Zhang
 * @deprecated
 */
@Deprecated
public class LevelAdapter {

    /**
     *
     */
    public LevelAdapter() {
        mPool = Executors.newCachedThreadPool();
    }
    /**
     * Our thread pool to do work on.
     */
    private final ExecutorService mPool;
    /**
     * The data to be sent out.
     */
    private Map<String, Object> mOutData;
    /**
     * The Future object containing the data that has been worked on for the
     * longest amount of time.
     */
    private Future<Map<String, Object>> mNextData;

    /**
     * Set the data to be copied over.
     *
     * @param data The data to be copied; WILL NOT BE TOUCHED
     */
    public void setData(Map<String, Object> data) {
        if (mNextData.isDone() && mOutData == null) {
            mNextData = mPool.submit(new Copier(data));
        }
    }

    /**
     * Gets the data that has been copied
     *
     * @return The copied data; MAY BE NULL if it has been retrieved previously
     */
    public Map<String, Object> getData() {
        if (mNextData.isDone()) {
            try {
                mOutData = mNextData.get();
            } catch (InterruptedException ex) {
                ex.getMessage();
            } catch (ExecutionException ex) {
                ex.getMessage();
            }
        }
        Map<String, Object> tmp = mOutData;
        mOutData = null;
        return tmp;
    }

    /**
     * Our very own little worker.
     */
    private static class Copier implements Callable<Map<String, Object>> {

        /**
         * Makes a new me.
         *
         * @param copyFrom Copying from
         */
        public Copier(Map<String, Object> copyFrom) {
            this.mCopyFrom = copyFrom;
        }
        /**
         * The map we will be copying FROM.
         */
        private Map<String, Object> mCopyFrom;

        /**
         *
         * @return
         */
        public Map<String, Object> call() {
            return new HashMap<String, Object>(mCopyFrom);
        }
    }
}
